//
//  CreateSuccessViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2018/12/26.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "CreateSuccessViewController.h"
#import "UITextView+WZB.h"
#import "WallectService.h"
#import "AESCrypt.h"
@interface CreateSuccessViewController ()
@property (nonatomic,strong) UIView *backViewFirst;
@property (nonatomic,strong) UIView *backViewSecond;
@property (nonatomic,strong) UILabel *tipLab;
@property (nonatomic,strong) UITextView *inputTV;
@property (nonatomic,assign) NSInteger steps;
@property (nonatomic,weak) XThemeFlatButton * doneBtn;
@property (nonatomic,weak) UIButton *copybtn;

@end

@implementation CreateSuccessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

-(void)setSteps:(NSInteger)steps{
    _steps = steps;
    if (steps == 2) {
        self.title = XLocalized(@"备份私钥");
        self.copybtn.hidden = NO;
        [self.doneBtn setTitle:XLocalized(@"下一步") forState:UIControlStateNormal];
        self.inputTV.editable = NO;
        self.inputTV.text = self.wallectService.ownerPrivateKey.eosPrivateKey;
        self.tipLab.text = XLocalized(@"立刻备份您的私钥");
    }else{
        self.title = XLocalized(@"确认私钥");
        self.copybtn.hidden = YES;
        [self.doneBtn setTitle:XLocalized(@"完成") forState:UIControlStateNormal];
        self.inputTV.editable = YES;
        self.inputTV.text = @"";
        self.inputTV.wzb_placeholder = XLocalized(@"请按照抄写的私钥填入输入框，确认您备份的私钥正确");
        self.tipLab.text = XLocalized(@"确认您的钱包私钥");
    }
}

- (void)backBtnClicked{
    kWeakSelf(self);
    [XAlertTool alertTitle:@"确定已经备份账号私钥?" message:nil cancelAction:^{
        
    } sureAction:^{
        [weakself.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)setupUI{
    self.fd_interactivePopDisabled = YES;
    self.backViewFirst = [UIView new];
    self.backViewFirst.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:self.backViewFirst];
    [self.backViewFirst mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0);
        make.top.mas_equalTo(kTopHeight);
    }];
    self.tipLab = [UILabel new];
    self.tipLab.textColor = UIColorHex(#333333);
    self.tipLab.font = kBoldFont(17);
    [self.backViewFirst addSubview:self.tipLab];
    [self.tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(AdaptedWidth(50));
    }];
    self.inputTV = [UITextView new];
    self.inputTV.layer.cornerRadius = AdaptedWidth(10);
    self.inputTV.font = kDefaultFont(14);
    self.inputTV.backgroundColor = UIColorHex(#F7F7F7);
    [self.backViewFirst addSubview:self.inputTV];
    [self.inputTV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(AdaptedWidth(34));
        make.right.mas_equalTo(-AdaptedWidth(34));
        make.height.mas_equalTo(AdaptedWidth(100));
        make.top.equalTo(self.tipLab.mas_bottom).offset(AdaptedWidth(40));
    }];
    UIButton *copyBtn = [UIButton new];
    self.copybtn=copyBtn;
    [self.backViewFirst addSubview:copyBtn];
    [copyBtn setImage:IMAGE_NAMED(@"copy_black") forState:UIControlStateNormal];
    [copyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.mas_equalTo(self.inputTV);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    kWeakSelf(self);
    [copyBtn addTapBlock:^(UIButton * _Nonnull btn) {
        if (weakself.inputTV.text.length) {
            UIPasteboard *pab = [UIPasteboard generalPasteboard];
            [pab setString:weakself.inputTV.text];
            if (pab == nil) {
                [weakself.view makeToast:XLocalized(@"复制失败!")];
            }else
            {
                [weakself.view makeToast:XLocalized(@"复制成功!")];
            }
        }
    }];
    
    XThemeFlatButton*doneBtn = [XThemeFlatButton button];
    self.doneBtn = doneBtn;
    [self.backViewFirst addSubview:doneBtn];
    [doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(40));
        make.top.equalTo(self.inputTV.mas_bottom).offset(AdaptedWidth(80));
    }];
    self.steps = 2;
    [self.doneBtn addTapBlock:^(UIButton * _Nonnull btn) {
        if (weakself.steps == 2) {
            [XAlertTool alertTitle:@"再次确认" message:@"BEES不会上传您的EOS钱包私钥,一旦丢失无法找回,请妥善保管" cancelAction:^{
                
            } sureAction:^{
                weakself.steps = 1;
            }];
        }else{
            DLog(@"创建成功  导入");
            
            if ([weakself.inputTV.text isEqualToString:weakself.wallectService.ownerPrivateKey.eosPrivateKey]) {
                [XAlertTool alertTitle:@"创建账号成功!是否立即导入钱包?" message:@"" cancelAction:^{
                } sureAction:^{
                    Wallet*wallet = [Wallet new];
                    wallet.wallet_name = weakself.wallectService.accountName;
                    wallet.wallet_uid =[[NSString stringWithFormat:@"%@%@",weakself.wallectService.accountName,self.wallectService.publicKey] sha256String];
                    wallet.password_sha = [weakself.wallectService.psw sha256String];
                    wallet.password_tip = weakself.wallectService.pswTip;
                    wallet.wallet_private_AES_key = [WalletTool encryptWalletPrivatekey:weakself.wallectService.ownerPrivateKey.eosPrivateKey password:weakself.wallectService.psw]; ;
                    wallet.wallet_public_key = weakself.wallectService.ownerPrivateKey.eosPublicKey;
                    wallet.wallet_system = kContractName_EOSIOTOKEN;
                    wallet.addTime = [TimeHelper getCurrentTimestamp];
                    BOOL success = [[XHDataBaseManager shareInstance]insertWalletToDB:wallet];
                    if (success) {
                        [weakself.view makeToast: XLocalized(@"导入成功!")];
                        [weakself.navigationController popToRootViewControllerAnimated:YES];
                    }else{
                        [weakself.view makeToast: XLocalized(@"导入失败!")];
                    }
                }];
            }else{
                [XAlertTool alertTitle:@"备份的私钥不正确，是否重新查看私钥？" message:@"" cancelAction:^{
                    
                } sureAction:^{
                    weakself.steps = 2;
                }];
            }
        }
    }];
    
    self.backViewSecond = [UIView new];
    self.backViewSecond.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:self.backViewSecond];
    [self.backViewSecond mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0);
        make.top.mas_equalTo(kTopHeight);
    }];
    UIImageView *iconV = [UIImageView new];
    iconV.image = IMAGE_NAMED(@"back_up");
    [self.backViewSecond addSubview:iconV];
    [iconV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptedWidth(50));
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(54), AdaptedWidth(52)));
    }];
    UILabel*tipL = [UILabel new];
    tipL.textColor = UIColorHex(#333333);
    tipL.font = kBoldFont(20);
    tipL.text = XLocalized(@"立刻备份您的私钥");
    [self.backViewSecond addSubview:tipL];
    [tipL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconV.mas_bottom).offset(AdaptedWidth(50));
        make.centerX.mas_equalTo(0);
    }];
    UILabel*tipL2 = [UILabel new];
    tipL2.numberOfLines = 0;
    tipL2.textColor = UIColorHex(#999999);
    tipL2.font = kDefaultFont(13);
    tipL2.text = XLocalized(@"备份钱包：抄写私钥，并保存在安全的地方。千万不要保存在网络上。然后尝试转入转出小额资产开始使用。");

    [self.backViewSecond addSubview:tipL2];
    [tipL2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tipL.mas_bottom).offset(AdaptedWidth(50));
        make.right.mas_equalTo(-AdaptedWidth(40));
        make.left.mas_equalTo(AdaptedWidth(40));
    }];
    
    XThemeFlatButton *backupBtn = [XThemeFlatButton button];
    [backupBtn setTitle:XLocalized(@"备份私钥") forState:UIControlStateNormal];
    [self.backViewSecond addSubview:backupBtn];
    [backupBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(40));
        make.top.equalTo(tipL2.mas_bottom).offset(AdaptedWidth(80));
    }];
    [backupBtn addTapBlock:^(UIButton * _Nonnull btn) {
        [weakself.backViewSecond removeFromSuperview];
    }];
}

@end
